tempFourLegsDetailData: FourLegsDetailData; 
update (Date - 1000000000*60*60*24) from `tempFourLegsDetailData where Date.minute > 20:00:00;
update (Date - 1000000000*60*60*24) from `tempFourLegsDetailData where Date.minute within 00:00:00 01:01:00; 

cuCU3MUSDCNHMeanReverseStrategy: tempFourLegsDetailData; //where Symbol = `cu1712DIDIHGZ7DIDI6DIDI2204.6226;

cuCU3MUSDCNHMeanReverseStrategy:-201#delete date,second from select Date:last Date, Symbol: last Symbol, BidPrice1: last BidPrice1, BidVol1: last BidVol1, AskPrice1: last AskPrice1, AskVol1: last AskVol1, LegOneBidPrice1: last LegOneBidPrice1, LegOneAskPrice1: last LegOneAskPrice1,LegTwoBidPrice1: last LegTwoBidPrice1, LegTwoAskPrice1: last LegTwoAskPrice1,LegThreeBidPrice1: last LegThreeBidPrice1, LegThreeAskPrice1: last LegThreeAskPrice1,LegFourBidPrice1: last LegFourBidPrice1, LegFourAskPrice1: last LegFourAskPrice1  by Date.date, 1 xbar Date.second from cuCU3MUSDCNHMeanReverseStrategy;


delete from `cuCU3MUSDCNHMeanReverseStrategy where Date.minute within  01:00:00 09:00:05;
delete from `cuCU3MUSDCNHMeanReverseStrategy where Date.minute within  10:15:00 10:30:05;
delete from `cuCU3MUSDCNHMeanReverseStrategy where Date.minute within  11:30:00 13:30:05;
delete from `cuCU3MUSDCNHMeanReverseStrategy where Date.minute within  15:00:00 21:00:05;

bollingerBands: {[k;n;data]      movingAvg: mavg[n;data];      md: sqrt mavg[n;data*data]-movingAvg*movingAvg;      movingAvg+/:(k*-1 0 1)*\:md};

BidPrice1Bollinger:{bollingerBands[1;200;x]} cuCU3MUSDCNHMeanReverseStrategy`BidPrice1;
AskPrice1Bollinger:{bollingerBands[1;200;x]} cuCU3MUSDCNHMeanReverseStrategy`AskPrice1;

update LowerBand:BidPrice1Bollinger[0], HigherBand:AskPrice1Bollinger[2] from `cuCU3MUSDCNHMeanReverseStrategy;

delete from `cuCU3MUSDCNHMeanReverseStrategy where HigherBand < 1;
delete from `cuCU3MUSDCNHMeanReverseStrategy where LowerBand < 1;

Signal: select from cuCU3MUSDCNHMeanReverseStrategy where  (BidPrice1 > HigherBand) or (AskPrice1 < LowerBand);
update Signal: `Long from `cuCU3MUSDCNHMeanReverseStrategy where AskPrice1 < LowerBand;
update Signal: `Short from `cuCU3MUSDCNHMeanReverseStrategy where BidPrice1 > HigherBand;


Signal: select Date, BidPrice1, AskPrice1, LegOneBidPrice1,LegOneAskPrice1, LegTwoBidPrice1,LegTwoAskPrice1, Signal, LowerBand, HigherBand from cuCU3MUSDCNHMeanReverseStrategy where Date = max Date; //where  (BidPrice1 > HigherBand) or (AskPrice1 < LowerBand);

Signal: select from Signal where Date = max Date;

//
////Second Problem
//FinalSignal,:Signal;
//
//FinalSignal: select from FinalSignal where ((Signal = `Long) or  (Signal = `Short));
//
//ShortLongFinalSignal: select distinct Date, BidPrice1, AskPrice1, LegOneBidPrice1,LegOneAskPrice1, LegTwoBidPrice1,LegTwoAskPrice1,Signal from FinalSignal
//
////End
//delete from `ShortLongFinalSignal where Date.minute within 09:00:00 09:00:05;
//delete from `ShortLongFinalSignal where Date.minute within 10:30:00 10:30:05;
//delete from `ShortLongFinalSignal where Date.minute within 13:30:00 13:30:05;
//delete from `ShortLongFinalSignal where Date.minute within 21:00:00 21:00:05;
//
//ShortLong: select from `ShortLongFinalSignal where (Signal <> (prev Signal));
//tempFinalSignal:select from ShortLongFinalSignal;
//update LowerBand:1.0, HigherBand:1.0 from `tempFinalSignal; 
//FinalSignal: tempFinalSignal;

FinalSignal:FinalSignal,Signal;

ShortLongFinalSignal: select distinct Date, BidPrice1, AskPrice1, LegOneBidPrice1,LegOneAskPrice1, LegTwoBidPrice1,LegTwoAskPrice1,Signal  from FinalSignal where ((Signal = `Long) or  (Signal = `Short));

delete from `ShortLongFinalSignal where Date.minute within 09:00:00 09:00:05;
delete from `ShortLongFinalSignal where Date.minute within 10:30:00 10:30:05;
delete from `ShortLongFinalSignal where Date.minute within 13:30:00 13:30:05;
delete from `ShortLongFinalSignal where Date.minute within 21:00:00 21:00:05;

ShortLong: select from `ShortLongFinalSignal where (Signal <> (prev Signal));
ShortLongFinalSignal:ShortLong;
FinalSignal:update LowerBand:1.0, HigherBand:1.0  from ShortLongFinalSignal;

